package emulator.hardware.io.peripherals;

import emulator.hardware.bits.SerialBit;
import emulator.hardware.bits.SharedBit;
import emulator.hardware.io.Via6522;

public class SerialPort implements IecPort {
	private SharedBit	serial_srq;
	private SerialBit	serial_atn;
	private SerialBit	serial_clk;
	private SerialBit	serial_data;

	public void attachToVia(Via6522 via_keyboard, Via6522 via_joystick)
	{
		serial_srq = via_keyboard.getPortB().getControlBit(0);
		serial_atn = new SerialBit(new SharedBit()); // Userport
		serial_clk = new SerialBit(via_joystick.getPortA().getInPortBit(0));
		serial_data = new SerialBit(via_joystick.getPortA().getInPortBit(1));
		via_keyboard.getPortB().setControlBit(0, serial_srq);
		via_joystick.getPortA().setOutPortBit(7, serial_atn);
		via_keyboard.getPortA().setControlBit(1, serial_clk);
		via_keyboard.getPortB().setControlBit(1, serial_data);
	}

	/* (non-Javadoc)
	 * @see emulator.hardware.io.peripherals.IecPort#getClk()
	 */
	@Override
	public boolean getClk() {
		return serial_clk.getInBit();
	}

	/* (non-Javadoc)
	 * @see emulator.hardware.io.peripherals.IecPort#getData()
	 */
	@Override
	public boolean getData() {
		return serial_data.getInBit();
	}

	/* (non-Javadoc)
	 * @see emulator.hardware.io.peripherals.IecPort#getAtn()
	 */
	@Override
	public boolean getAtn() {
		return serial_atn.getInBit();
	}

	/* (non-Javadoc)
	 * @see emulator.hardware.io.peripherals.IecPort#setBusData(boolean)
	 */
	@Override
	public void setBusData(boolean value) {
		serial_data.setBusBit(value);
	}

	/* (non-Javadoc)
	 * @see emulator.hardware.io.peripherals.IecPort#setBusClk(boolean)
	 */
	@Override
	public void setBusClk(boolean value) {
		serial_clk.setBusBit(value);
	}
}
